<template>
  <div class="login-container">
    <div class="logo-box">
      <img src="../assets/img/logo_word.png" alt="" />
    </div>
    <div class="plateform-name">
      <div class="title">纺织云平台</div>
      <div class="desc">开拓创新 追求卓越 诚信为本 客户至上</div>
    </div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">欢迎登录</h3>
      </div>
      <el-form-item prop="username" class="display">
        <div class="label-l">登录名</div>
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="admin_account"
          type="text"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item prop="password" class="display">
        <div class="label-l">密码</div>
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
          name="admin_password"
          auto-complete="off"
          @keyup.enter.native="handleLogin('loginForm')"
        />
      </el-form-item>
      <div class="btnBox">
        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%;margin-top:40px;height:50px"
          @click.native.prevent="handleLogin('loginForm')"
          >登录</el-button
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { isvalidNull } from '@/utils/validate.js';
import { Login } from '@/api/api.js';
import { setSession } from '@/utils/auth.js';
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidNull(value)) {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false
    };
  },
  created() {},
  destroyed() {},
  methods: {
    handleLogin(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.loading = true;
          Login(this.loginForm)
            .then(res => {
              if (res.code === 1) {
                this.$store.commit('token', res.data);
                setSession('token', res.data);
                setTimeout(() => {
                  this.$router.push('/WorkChart');
                  this.loading = false;
                }, 1000);
              } else {
                this.loading = false;
                this.$notify.error({
                  title: '错误',
                  message: res.msg
                });
              }
            })
            .catch(error => {
              this.loading = false;
            });
        }
      });
    }
  }
};
</script>

<style lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  position: relative;
  background: url(../assets/img/login_bg.png) no-repeat;
  background-size: 100% 100%;
  .logo-box {
    width: 227px;
    height: 48px;
    position: absolute;
    top: 40px;
    left: 40px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .plateform-name {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 140px;
    .title {
      font-size: 40px;
      font-weight: 600;
      color: #ffffff;
    }
    .desc {
      font-size: 26px;
      font-weight: 400;
      color: #ffffff;
      margin-top: 12px;
    }
  }
  .login-form {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 110px;
    width: 395px;
    height: 490px;
    background: #f9fdff;
    border-radius: 10px;
    padding: 0 30px;
    box-sizing: border-box;
    .title-container {
      margin-top: 60px;
      .title {
        font-size: 30px;
        font-weight: 600;
        color: #222f43;
        text-align: center;
      }
      margin-bottom: 70px;
    }
    .el-form-item__content {
      display: flex;
      align-items: center;
      height: 50px;
      background: #ffffff;
      border-radius: 2px;
      border: 1px solid #e6e6e6;
      .label-l {
        font-size: 16px;
        font-weight: 400;
        color: #222f43;
        width: 60px;
        padding-left: 20px;
        flex-shrink: 0;
      }
      input {
        border: 0;
        font-size: 16px;
        color: #222f43;
      }
    }
  }
}
</style>
